#ifndef LIBRARY_H
#define LIBRARY_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include <ctype.h>
#include <sqlite3.h>

// 常量定义
#define MAX_NAME_LEN 50
#define MAX_bookname_LEN 100
#define MAX_AUTHOR_LEN 50
#define MAX_PUBLISHER_LEN 50
#define MAX_CATEGORY_LEN 30
#define ROOT_PASSWORD "root123455"
#define DATABASE_FILE "library.db"
#define LEN 512

const char *insert_into_user_table;
const char *create_user_table;
const char *create_book_table;
const char *create_record_table;

// 验证码状态
typedef struct {
    int code;               // 验证码
    time_t generate_time;   // 生成时间
    int error_attempts;     // 错误次数
    int is_valid;           // 是否有效
} VerifyCode;

VerifyCode verify_code ;  // 全局验证码状态



// 用户权限枚举
typedef enum {
    USER_NORMAL,
    USER_ROOT,
    USER_BLACKLIST=-1
} UserPower;

// 图书状态枚举
typedef enum {
    BOOK_AVAILABLE,
    BOOK_BORROWED,
    BOOK_LOST
} Booktatus;

// 用户结构体
typedef struct User {
    int id;
    char username[MAX_NAME_LEN];
    char password[MAX_NAME_LEN];
    UserPower power;//权限
} User;

// 图书结构体
typedef struct Book {
    int id;
    char bookname[MAX_bookname_LEN];
    char author[MAX_AUTHOR_LEN];
    char publisher[MAX_PUBLISHER_LEN];
    char category[MAX_CATEGORY_LEN];
    int years;
    Booktatus status;
    int borrowerId;
    time_t borrowDate;
    time_t returnDate;
} Book;



// 全局变量声明
extern sqlite3 *db;
extern User *currentUser;

// 函数声明

// 黑名单管理菜单
void BlacklistManagementMenu();

// 将用户加入黑名单
void addToBlacklist(const char* username);

// 将用户移出黑名单
void removeFromBlacklist(const char* username);

// 列出所有黑名单用户
void listBlacklistedUsers();

void initSystem();
void createTables();
void mainMenu();
void AdminMenu(const char * username);
void UserMenu(const char * username);
void ChangePassword(const char * username, int is_forced);
void Login();
void AdminLogin();

void listAllUser();
void addBook();
void deleteBook();
void modifyBook();
void searchBook();
void borrowBook();
void returnBook();
void viewBorrowHistory();
void changeUserPower();
void listAllBook();
void viewUserInfo();
void logout();
void clearInputBuffer();

void printBookDetails(Book* book);
void printUserDetails(User* user);

int AreYouOk(const char *db_name);
void MainMenu();
void Regist();
int CheckRepeat(const char*username);
int InsertUserTable(const char *username,const char* password,const char *sex);
int generate_verify_code();
void send_mock_verify_code(const char* account);
int verify_verify_code(int input_code);


int CheckUserInfo(const char*username,const char*password);
int CheckCallBack(void *data, int argc, char **argv, char **colname);
void SelectMenu(const char * username);
void PersonalInfo(const char* username);
void BookBorrow(const char*username);
int BorrowRecord(const char *username,const char *bookname,int num);
void RecordHistory(const char *username);
void BookReturn(const char* username);
int ReturnCallBack(void *data, int arc, char **arv, char **column);
int AddReturnRecord(int choice,const char *bookname);
long manual_time_diff(const char* start_time, const char* end_time);


void UserManagementMenu();
void ViewAllUsers();
void ViewUserDetails(const char*username);
void ModifyUserInfo();
void ResetUserPassword() ;
void DeleteUser();
void ManageBooks();



void AddBook();
void ModifyBookInfo();
void DeleteBookInfo() ;
void SearchBookInfo();
void ViewRecords();






// 在常量定义部分添加
#define MAX_REVIEW_CONTENT 500

// 在表定义部分添加
const char *create_review_table = "CREATE TABLE IF NOT EXISTS review ("
    "id INTEGER PRIMARY KEY AUTOINCREMENT,"
    "user_id INTEGER NOT NULL,"
    "book_id INTEGER NOT NULL,"
    "username TEXT NOT NULL,"
    "content TEXT NOT NULL,"
    "rating INTEGER NOT NULL CHECK(rating >= 1 AND rating <= 5),"
    "review_time INTEGER NOT NULL,"
    "FOREIGN KEY(user_id) REFERENCES user(id),"
    "FOREIGN KEY(book_id) REFERENCES book(id)"
");";

// 在函数声明部分添加
void addReview(const char *username);
void viewBookReviews(const char *bookname);
void viewUserReviews(const char *username);


void listAllBooks();
#endif // LIBRARY_H




